﻿using DigitalHealth.PACS.ChatWebSocket.Context;
using DigitalHealth.PACS.ChatWebSocket.Services;
using DigitalHealth.PACS.Model;
using System.Collections.Generic;
using System.Linq;

namespace DigitalHealth.PACS.ChatWebSocket.Repositories.Imp
{

    internal class ChatGroupMessageRepository : IChatGroupMessageRepository
    {
        public ChatGroupMessageRepository(ChatContext chatContext)
        {
            ChatContext = chatContext;
        }

        public ChatContext ChatContext { get; set; }
        public bool ChatMessageExists(string id)
        {
            return ChatContext.ChatGroupMessages.Any(c=>c.Id.ToString()==id);
        }

        public void Create(ChatGroupMessage chatMessage)
        {
            ChatContext.ChatGroupMessages.Add(chatMessage);
            ChatContext.SaveChanges();
        }

        public void Delete(ChatGroupMessage chatMessage)
        {
            ChatContext.ChatGroupMessages.Remove(chatMessage);
            ChatContext.SaveChanges();
        }

        public ChatGroupMessage GetChatMessageById(string id)
        {
            return ChatContext.ChatGroupMessages.Find(id);
        }

        public IEnumerable<ChatGroupMessage> GetChatMessages()
        {
            return ChatContext.ChatGroupMessages.ToList();
        }

        public IEnumerable<ChatGroupMessage> GetChatMessagesByReceiverOrg(Organization receiverOrg)
        {
            return ChatContext.ChatGroupMessages.Where(c => c.ReceiverOrg == receiverOrg);
        }

        public IEnumerable<ChatGroupMessage> GetNotReadChatMessagesByReceiverOrg(Organization receiverOrg)
        {
            return ChatContext.ChatGroupMessages.Where(c => c.ReceiverOrg == receiverOrg && c.State == Model.Enumeration.MsgState.未读);
        }

        public void Update(ChatGroupMessage chatMessage)
        {
            ChatContext.ChatGroupMessages.Update(chatMessage);
            ChatContext.SaveChanges();
        }
    }
}
